<?php
define("APPTAPH", WEBROOT."/app");
define("APPFW", WEBROOT."/framework");
define("APPLIB",APPFW."/lib");

define("CONFIG_PATH", APPTAPH."/config");

include_once APPFW.'/Contorller.php';
include_once APPLIB.'/View.php';

class Fw{
	public $param;
	public $appName;
	public $baseUrl = '';
	public $_date = null;

	private $_scriptUrl=null;
	private $_baseUrl=null;
	private $language;
	private $_session =null;
	private $_requestUri;
	private static $_object ;
	
	
	public $_excel = null;
	
	private  static $_instance;

	public function Fw(){
		$this->_session = $this->load('session');
	}

	public function setAppName(){
		$this->appName = $this->param["appName"];
	}
	
	public static function app(){
		if(!(self::$_instance instanceof self) ) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}

	/*
	 * 设用控制器类
	 * */
	public function run($c=null,$a=null){
	   $action = $this->getContorllerAction($c,$a);
	   
	   $contorller =  ucfirst($action["contorller"]);
	   
	   include_once APPTAPH.'/contorller/'.$contorller.'Contorller.php';
	   //new Site();
	   $actionMethod = $action["action"]."Action";
	   $contorllerObject = new $contorller;
	  
	  if(method_exists($contorllerObject,$actionMethod)){
	  	   $contorllerObject->{$actionMethod}();
	  }else{
	  	echo "ACTION NOT FIND";
	  }
	}

	/**
	 * 读取配置文件
	 * @param string $url
	 */
	public function param($url){
	     $this->param = include_once $url;	
	     $this->setAppName();
	     $this->baseUrl=$this->getBaseUrl();
	     $this->getLanguage();
	}

	/**
	 * 获收 控制器类 和action 方法
	 * @return Array()
	 */
	public function getContorllerAction($c = null ,$a=null){
        $action= array();
        if(!empty($a)&&!empty($c)){
        	$action["contorller"] =$c;
        	$action["action"] = $a;
        }else if(empty($_REQUEST["r"])){
         	 $action["contorller"]=strtolower($this->param["defineContorller"]);
         	 $action["action"]='index';
         }else{
         	$temp = explode('/',$_REQUEST["r"]);
         	$action["contorller"] = $temp[0];
         	$action["action"] = empty($temp[1])?'index':strtolower($temp[1]);
         }
		return $action;
	}
	
	public function  load($className){
		require_once APPLIB.'/'.$className.'.php';
		if(empty($this->_object[$className])){
			$className = "fw_".$className;
		
			$this->_object[$className] = new $className;
		}
		return  $this->_object[$className];
	}
	
	public  function log($string,$title){
		
	}
	
	
	public function setHostInfo($value){
		$this->_hostInfo=rtrim($value,'/');
	}
	
	public function getBaseUrl($absolute=false){
		if($this->_baseUrl===null)
			$this->_baseUrl=rtrim(dirname($this->getScriptUrl()),'\\/');
		return $absolute ? $this->getHostInfo() . $this->_baseUrl : $this->_baseUrl;
	}
	
	public function getScriptUrl(){
		if($this->_scriptUrl===null){
			$scriptName=basename($_SERVER['SCRIPT_FILENAME']);
			if(basename($_SERVER['SCRIPT_NAME'])===$scriptName)
				$this->_scriptUrl=$_SERVER['SCRIPT_NAME'];
			elseif(basename($_SERVER['PHP_SELF'])===$scriptName)
			$this->_scriptUrl=$_SERVER['PHP_SELF'];
			elseif(isset($_SERVER['ORIG_SCRIPT_NAME']) && basename($_SERVER['ORIG_SCRIPT_NAME'])===$scriptName)
			$this->_scriptUrl=$_SERVER['ORIG_SCRIPT_NAME'];
			elseif(($pos=strpos($_SERVER['PHP_SELF'],'/'.$scriptName))!==false)
			$this->_scriptUrl=substr($_SERVER['SCRIPT_NAME'],0,$pos).'/'.$scriptName;
			elseif(isset($_SERVER['DOCUMENT_ROOT']) && strpos($_SERVER['SCRIPT_FILENAME'],$_SERVER['DOCUMENT_ROOT'])===0)
			$this->_scriptUrl=str_replace('\\','/',str_replace($_SERVER['DOCUMENT_ROOT'],'',$_SERVER['SCRIPT_FILENAME']));
			else;
				#throw new CException(Yii::t('yii','CHttpRequest is unable to determine the entry script URL.'));
		}
		return $this->_scriptUrl;
	}
	
	public function i18n($str){
		 if(empty($this->language)){
		 	 return $str;
		 }else{
		 	if(isset($this->language["{$str}"])){
		 		return $this->language["{$str}"];
		 	}else return $str;
		 }
	}
	
	public function getLanguage(){
		if(file_exists(WEBROOT."/language/".$this->param["language"].'/language.php')){
			$this->language =  include_once WEBROOT."/language/".$this->param["language"].'/language.php';
		}else return NUll;
	}
	
	public function setSession($key,$value){
		if(empty($this->_session)){
		 	$this->_session = $this->load('session');
		}
		$this->_session->setSession($key,$value);
	}
	
	public function getSession($key,$k=null){
		$session = $this->_session->getSession($key);
		 if(empty($k)){
		 	return $session;
		 }else{
		 	return $session[$k];
		 }
		 
	}
	
	public function delSession($key){
		unset($_SESSION[$key]);
	}
	
	
	public function getRequestUri(){
		if($this->_requestUri===null){
			if(isset($_SERVER['HTTP_X_REWRITE_URL'])) // IIS
				$this->_requestUri=$_SERVER['HTTP_X_REWRITE_URL'];
			elseif(isset($_SERVER['REQUEST_URI'])){
				$this->_requestUri=$_SERVER['REQUEST_URI'];
				if(!empty($_SERVER['HTTP_HOST'])){
					if(strpos($this->_requestUri,$_SERVER['HTTP_HOST'])!==false)
						$this->_requestUri=preg_replace('/^\w+:\/\/[^\/]+/','',$this->_requestUri);
				}
				else
					$this->_requestUri=preg_replace('/^(http|https):\/\/[^\/]+/i','',$this->_requestUri);
			}
			elseif(isset($_SERVER['ORIG_PATH_INFO'])){  // IIS 5.0 CGI
				$this->_requestUri=$_SERVER['ORIG_PATH_INFO'];
				if(!empty($_SERVER['QUERY_STRING']))
					$this->_requestUri.='?'.$_SERVER['QUERY_STRING'];
			}else;
				#throw new Exception();
		}
	
		return $this->_requestUri;
	}
	
	public function getUrl(){
		   $requestUri = $this->getRequestUri();
		   $url = explode('?', $requestUri);
		   return $url[0];
	}


}